//异常错误统一处理
import 'package:common_utils/common_utils.dart';
import 'package:dio/dio.dart';
import 'package:part_time_job_pro/common/api/report.dart';
import 'package:part_time_job_pro/common/values/respones_code.dart';

ReportApi apiUtil = ReportApi();

//全局异常处理
class ExceptionHandler {
  // 处理 Dio 异常
  static Map<String, dynamic> handleDioError(DioException error) {
    Map<String, dynamic> result = {};
    //错误代码
    int errorCode = 0;
    //错误信息
    String errorMsg = "";
    //打印错误信息
    LogUtil.e("handleError:: error >> $error", tag: "异常捕获");
    if (error.response != null) {
      LogUtil.e("响应数据为: >> ${error.response}", tag: "异常捕获");
    }

    switch (error.type) {
      case DioExceptionType.connectionTimeout:
        errorCode = ResponesCode.connectTimeout;
        errorMsg = ResponesCode.connectTimeoutMsg;
        break;
      case DioExceptionType.receiveTimeout:
        errorCode = ResponesCode.receiveTimeout;
        errorMsg = ResponesCode.receiveTimeoutMsg;
        break;
      case DioExceptionType.sendTimeout:
        errorCode = ResponesCode.sendTimeout;
        errorMsg = ResponesCode.sendTimeoutMsg;
        break;
      case DioExceptionType.badResponse:
        errorCode = ResponesCode.badResponse;
        errorMsg = ResponesCode.badResponseMsg;
        break;
      case DioExceptionType.badCertificate:
        errorCode = ResponesCode.badCertificateError;
        errorMsg = ResponesCode.badCertificateErrorMsg;
        break;
      case DioExceptionType.connectionError:
        errorCode = ResponesCode.connectionError;
        errorMsg = ResponesCode.connectionErrorMsg;
        break;
      case DioExceptionType.cancel:
        errorCode = ResponesCode.cancelError;
        errorMsg = ResponesCode.cancelErrorMsg;
        break;
      case DioExceptionType.unknown:
        errorCode = ResponesCode.unknownError;
        errorMsg = ResponesCode.unknownErrorMsg;
        break;
      default:
        errorCode = ResponesCode.serviceError;
        errorMsg = ResponesCode.serviceErrorMsg;
    }

    result[ResponesCode.codeTag] = errorCode;
    result[ResponesCode.msgTag] = errorMsg;
    LogUtil.e(errorMsg, tag: "请求响应错误信息====>");
    return result;
  }

  // 处理 Http 错误码
  static handleHttpError(errorCode) {
    String errorMsg;
    Map<String, dynamic> result = {};
    switch (errorCode) {
      case 400:
        errorMsg = '请求语法错误';
        break;
      case 401:
        errorMsg = '未授权，请登录';
        break;
      case 403:
        errorMsg = '拒绝访问';
        break;
      case 404:
        errorMsg = '请求出错';
        break;
      case 408:
        errorMsg = '请求超时';
        break;
      case 500:
        errorMsg = '服务器异常';
        break;
      case 501:
        errorMsg = '服务未实现';
        break;
      case 502:
        errorMsg = '网关错误';
        break;
      case 503:
        errorMsg = '服务不可用';
        break;
      case 504:
        errorMsg = '网关超时';
        break;
      case 505:
        errorMsg = '不支持HTTP协议请求';
        break;
      default:
        errorMsg = '请求失败，错误码：$errorCode';
    }
    result[ResponesCode.codeTag] = errorCode;
    result[ResponesCode.msgTag] = errorMsg;
    LogUtil.e(errorMsg, tag: "HTTP错误信息====>");
  }

  //数据错误处理
  static Map<String, dynamic> handleDataException(e) {
    int errorCode = ResponesCode.dataParseError;
    String errorMsg = ResponesCode.dataParseErrorMSG;
    Map<String, dynamic> errorMap = {};
    LogUtil.e("【错误日志】===.>>>>>>$e");
    errorMap['code'] = errorCode;
    errorMap['msg'] = errorMsg;
    LogUtil.e("请求异常错误信息：===$errorMap");
    //上报日志
    //reportErrorMsg("0", e.toString(), errorMsg + "[$errorCode]");
    return errorMap;
  }

  //处理统一上报错误信息
  static reportErrorMsg(String businessType, String errorMsg, String title,
      {String? param, String? status, String? method}) {
    try {
      String errorMeta = '[${DateTime.now().toString()}][report_logs]';
      apiUtil.reportErrorStack((data) {
        int resultCode = data[ResponesCode.codeTag];
        var resultMsg = data[ResponesCode.msgTag];
        if (resultCode == ResponesCode.successCode) {
          LogUtil.e("[${DateTime.now().toString()}] 日志上报服务器成功!!!");
        } else {
          LogUtil.e("[${DateTime.now().toString()}] 日志上报服务器失败:$resultMsg");
        }
      }, businessType, "$errorMeta====>[$errorMsg]", title,
          status: status, method: method, param: param);
    } catch (e) {
      LogUtil.e("上报日志出错!");
    }
  }
}
